package com.xiaohua.xiaohuamall.ware.service.impl;

import com.xiaohua.common.utils.R;
import com.xiaohua.xiaohuamall.ware.feign.ProductFeignService;
import com.xiaohua.xiaohuamall.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohua.common.utils.PageUtils;
import com.xiaohua.common.utils.Query;

import com.xiaohua.xiaohuamall.ware.dao.WareSkuDao;
import com.xiaohua.xiaohuamall.ware.entity.WareSkuEntity;
import com.xiaohua.xiaohuamall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    WareSkuDao wareSkuDao;
    @Autowired
    ProductFeignService productFeignService;
    /**
     * skuId:
     * wareId:
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(params.get("skuId"))){
            queryWrapper.eq("sku_id",params.get("skuId"));
        }
        if (!StringUtils.isEmpty(params.get("wareId"))){
            queryWrapper.eq("ware_id",params.get("wareId"));
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1、判断如果还没有这个库存记录就新增
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("Ware_id", wareId));
        if (entities!=null &&entities.size() > 0){
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }else {
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            //TODO 远程查询sku的名字,如果失败，整个事务无需回滚
            //1、自己catch异常
            try {
                R info = productFeignService.info(skuId);
               Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0){
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            wareSkuDao.insert(skuEntity);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            //查询当前sku的总库存量
            //SELECT SUM(stock-stock_locked) FROM wms_ware_sku WHERE sku_id = 1
            Long count = baseMapper.getSkuStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock(count==null?false:count > 0);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

}